package StudyCode.summary.myfile;

import java.io.ByteArrayOutputStream;
import java.util.Arrays;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class TwoHexadecimalDigits {
	public static void main(String[] argv) {
		int integer = 2009000;
		System.out.println(Arrays.toString(convert(integer)));
	}

	private static byte[] convert(final int integer) {
		return convert(Integer.toHexString(integer));
	}

	private static byte[] convert(String digits) {

		if (digits.length() % 2 != 0) {
			digits = "0" + digits;
		}

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		for (int i = 0; i < digits.length(); i += 2) {
			final char c1 = digits.charAt(i);
			final char c2 = digits.charAt(i + 1);
			byte b = 0;
			if (c1 >= '0' && c1 <= '9') {
				b += (c1 - '0') * 16;
			} else if (c1 >= 'a' && c1 <= 'f') {
				b += (c1 - 'a' + 10) * 16;
			} else if (c1 >= 'A' && c1 <= 'F') {
				b += (c1 - 'A' + 10) * 16;
			} else {
				throw new IllegalArgumentException("非法的十六进制数字");
			}

			if (c2 >= '0' && c2 <= '9') {
				b += c2 - '0';
			} else if (c2 >= 'a' && c2 <= 'f') {
				b += c2 - 'a' + 10;
			} else if (c2 >= 'A' && c2 <= 'F') {
				b += c2 - 'A' + 10;
			} else {
				throw new IllegalArgumentException("非法的十六进制数字");
			}
			baos.write(b);
		}
		return baos.toByteArray();
	}
}
